/* 
    Primarily this tool is used in conjunction with http://search.twitter.com by creating a search RSS url. This will "watch" for search terms and deliver the related post to your IM. 
    Copyright (C) 2008  Roger Filomeno

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.godie.bot;
import java.lang.Thread;
import java.util.Stack;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;



class imbot implements Runnable {
	
	private String host;
	private String domain;
	private String loginname;
	private String password;
	private String peer;
	
	private Stack<String> messagequeue;
	imbot(String host,String domain, String loginname, String password, String peer) {
		
		this.host = host;
		this.domain = domain;
		this.loginname = loginname;
		this.password = password;
		this.peer = peer;
		
		this.messagequeue = new Stack<String>();
        Thread t = new Thread(this);
        t.start();
    }
	
    public void run() {
    	ConnectionConfiguration connConfig = new ConnectionConfiguration(this.host, 5222, this.domain);
        XMPPConnection connection = new XMPPConnection(connConfig);

	    try
	    {
	    	connection.connect();
        	connection.login(this.loginname,this.password);
        	Presence presence = new Presence(Presence.Type.available);  
	        connection.sendPacket(presence);
	        ChatManager chatmanager = connection.getChatManager();
	        Chat chat = chatmanager.createChat(this.peer, new imbotlistener());
	        
        	while(connection.isConnected()){
		        try {
		        	if (!this.messagequeue.empty())
		        	{
		        		String newmessage = this.messagequeue.pop(); 
			            Message msg = new Message(this.peer, Message.Type.chat);
			            msg.setBody(newmessage);
			            chat.sendMessage(msg);
		        	}
		        } catch (XMPPException e) {
		            //do nothing
		        }
		        
		        try
		    	{
		        	if (this.messagequeue.empty()) Thread.sleep(4000);
		        	Thread.sleep(1000);
		    	} 
		    	catch (Exception ee) 
		    	{
		    		ee.printStackTrace();
		    	}
        	}
	    }
	    catch (XMPPException e)
	    {
	    	try
	    	{
	    		Thread.sleep(10000);
	    	} 
	    	catch (Exception ee) 
	    	{
	    		ee.printStackTrace();
	    	}
	    	e.printStackTrace();
	    }
	    finally
	    {
	    	this.run();
	    }
    }
    
    public static class imbotlistener implements MessageListener {
        public void processMessage(Chat chat, Message message) {
        	//do nothing
        }
    }
    
    public void addmessage(String message)
    {
    	this.messagequeue.push(message);
    }
}




